﻿<script type="text/javascript" src="${base}/scripts/base/stringOperate.js"></script>
<script type="text/javascript">
$(function () {
    //加载成功公司
    $("#jgd_cggs").gsjgtree(1);

    //加载仓库
    $('#jgd_ckmc').combobox({
        url: $.webPath() + '/organ/ck/sskcCkmcForSskc',
        valueField: 'id',
        textField: 'mc',
        panelHeight: '200',
        editable: true
    });

    // 加载库存信息
    var selectedGrid = $("#search-jgd-selected-jgd-grid").edatagrid({
        fitColumns: false,
        rownumbers: true,
        singleSelect: true,
        pageNumber: 1,
        fit: true,
        frozenColumns: [
            [
                {field: 'khbm', align: 'center', title: '卡号'},
                {field: 'rkdh', align: 'center', title: '入库单号'},
                {field: 'code', align: 'center', title: '物资编码'},
                {field: 'name', align: 'center', title: '物资名称'},
                {field: 'ggxh', align: 'center', title: '规格型号'},
                {field: 'cd', align: 'center', title: '产地'},
                {field: 'dw', align: 'center', title: '单位'},
                {field: 'sku', align: 'center', title: '标识'}
            ]
        ],
        columns: [
            [
                {field: 'zl', title: '重量', align: 'right',
                    formatter: function (value) {
                        var val = formatSlForHtml(value);
                        return val;
                    }},
                {field: 'js', align: 'right', title: '件数'},
                {field: 'jz', align: 'right', title: '件重'},
                {field: 'kkdsl', align: 'right', title: '可开单重量',
                    formatter: function (value) {
                        var val = formatSlForHtml(value);
                        return val;
                    }},
                {field: 'wsdj', align: 'right', title: '无税单价(元)',
                    formatter: function (value) {
                        var val = formatJeForHtml(value);
                        return val;
                    }
                },
                {field: 'wsje', align: 'right', title: '无税金额(元)',
                    formatter: function (value) {
                        var val = formatJeForHtml2(value);
                        return val;
                    }
                },
                {field: 'cgdh', align: 'center', title: '采购单号'},
                {field: 'ck', align: 'center', title: '仓库'},
                {field: "gys", align: 'center', title: "供应商", width: 150},
                {field: 'sbgs', align: 'center', title: '申报公司'},
                {field: "cggs", align: 'center', title: "采购公司"},
                {field: "rkrq", align: 'center', title: "入库日期"}
            ]
        ],
        onSelect: function (rowIndex, rowData) {
            //获取每个单元格
            $("#search-jgd-selected-jgd-grid").edatagrid('editRow', rowIndex);
            var editors = $("#search-jgd-selected-jgd-grid").datagrid('getEditors', rowIndex);
            if (editors.length == 0)
                return;
        },
        onRowContextMenu: function (e, rowIndex, rowData) {
            e.preventDefault();
            $('#search-jgd-menu').menu('show', {
                left: e.pageX,
                top: e.pageY
            }).data("rowIndex", rowIndex);
        }
    });

    $("#jgd_into_grid").edatagrid({
        url: $.webPath() + "/kcgl/sskc/jgdKcList",
        fitColumns: false,
        singleSelect: true,
        toolbar: "#toolbar_jgd_search",
        fit: true,
        pagination: true,
        checkOnSelect: false,
        selectOnCheck: false,
        pageNumber: 1,
        rownumbers: true,
        frozenColumns: [
            [
                {field: 'khbm', align: 'center', title: '卡号'},
                {field: 'rkdh', align: 'center', title: '入库单号'},
                {field: 'code', align: 'center', title: '物资编码'},
                {field: 'name', align: 'center', title: '物资名称'},
                {field: 'ggxh', align: 'center', title: '规格型号'},
                {field: 'cd', align: 'center', title: '产地'},
                {field: 'dw', align: 'center', title: '单位'},
                {field: 'sku', align: 'center', title: '标识'}
            ]
        ],
        columns: [
            [
                {field: 'zl', title: '重量', align: 'right',
                    formatter: function (value) {
                        var val = formatSlForHtml(value);
                        return val;
                    }},
                {field: 'js', align: 'right', title: '件数'},
                {field: 'jz', align: 'right', title: '件重'},
                {field: 'kkdsl', align: 'right', title: '可开单重量',
                    formatter: function (value) {
                        var val = formatSlForHtml(value);
                        return val;
                    }},
                {field: 'wsdj', align: 'right', title: '无税单价(元)',
                    formatter: function (value) {
                        var val = formatJeForHtml(value);
                        return val;
                    }
                },
                {field: 'wsje', align: 'right', title: '无税金额(元)',
                    formatter: function (value) {
                        var val = formatJeForHtml2(value);
                        return val;
                    }
                },
                {field: 'cgdh', align: 'center', title: '采购单号'},
                {field: 'ck', align: 'center', title: '仓库'},
                {field: "gys", align: 'center', title: "供应商", width: 150},
                {field: 'sbgs', align: 'center', title: '申报公司'},
                {field: "cggs", align: 'center', title: "采购公司"},
                {field: "rkrq", align: 'center', title: "入库日期"}
            ]
        ],
        onSelect: function (rowIndex, rowData) {
            var data = $('#search-jgd-selected-jgd-grid').datagrid("getData");
            var flag = true;
            var flag1 = true;
            var flag2 = true;
            var flag3 = true;
            // 判断可开单数量是否小于0
            if (rowData.kkdsl <= 0) {
                flag = false;
            } else {
                // 判断是否重复添加
                $.each(data.rows, function (i, val) {
                    if (val.khbm == rowData.khbm) {
                        flag1 = false;
                        return false;
                    }
                })
                // 判断申报公司是否相同
                if(flag1){
                    $.each(data.rows, function (i, val) {
                        if (val.sbgs != rowData.sbgs) {
                            flag2 = false;
                            return false;
                        }
                    })
                }
                // 判断是否同一批次
                if(flag1&&flag2){
                    $.each(data.rows, function (i, val) {
                        if (val.rkdh != rowData.rkdh) {
                            flag3 = false;
                            return false;
                        }
                    })
                }
            }
            if (!flag) {
                $.messager.alert('提示', "可开单重量不大于0!请重新选择", 'info');
            } else {
                if (!flag1) {
                    $.messager.alert('提示', "选择的物资与已选过!请重新选择", 'info');
                } else if(!flag2){
                    $.messager.alert('提示', "选择的物资必须申报公司相同!请重新选择", 'info');
                }else if(!flag3){
                    $.messager.alert('提示', "选择的物资不是同一批次!请重新选择", 'info');
                }else{
                    $(selectedGrid).datagrid('appendRow', rowData);
                }
            }
        }
    });

    $("#search-jgd-menu").menu({
        onClick: function (item) {
            if (item.name == "delete") {
                var data = $("#search-jgd-selected-jgd-grid").datagrid('getData');
                var row = data.rows[$(this).data("rowIndex")];
                $("#search-jgd-selected-jgd-grid").datagrid('deleteRow', $(this).data("rowIndex"));
            }
        }
    });

    $("#btn_jgd_search_init").click(function () {
        var sbgsJc = $('#jgd_sbgs').val() == null ? null : $('#jgd_sbgs').val();
        var cggsJc = $('#jgd_cggs').combotree('getValue') == null ? null : $('#jgd_cggs').combotree('getValue');
        var ckId = $('#jgd_ckmc').combotree('getValue') == null ? null : $('#jgd_ckmc').combotree('getValue');
        var ckmc = $('#jgd_ckmc').val() == null ? null : $('#jgd_ckmc').val();
        var code = $('#jgd_code').val() == null ? null : $('#jgd_code').val();
        var jtwzname = $('#jgd_ggxh').val() == null ? null : $('#jgd_ggxh').val();
        var khbm = $('#jgd_khbm').val() == null ? null : $('#jgd_khbm').val();
        var wzmc = $('#jgd_wzmc').val() == null ? null : $('#jgd_wzmc').val();
        var rkdh = $('#jgd_rkdh').val() == null ? null : $('#jgd_rkdh').val();

        $('#jgd_into_grid').edatagrid({
            url: $.webPath() + "/kcgl/sskc/jgdKcList",
            pageNumber: 1,
            queryParams: {
                ckmc: ckmc,
                ckId: ckId,
                jtwzname: jtwzname,
                khbm: khbm,
                qk: "0",
                code: code,
                wzmc: wzmc,
                sbgsJc: sbgsJc,
                cggsJc: cggsJc,
                rkdh:rkdh
            },
            frozenColumns: [
                [
                    {field: 'khbm', align: 'center', title: '卡号'},
                    {field: 'rkdh', align: 'center', title: '入库单号'},
                    {field: 'code', align: 'center', title: '物资编码'},
                    {field: 'name', align: 'center', title: '物资名称'},
                    {field: 'ggxh', align: 'center', title: '规格型号'},
                    {field: 'cd', align: 'center', title: '产地'},
                    {field: 'dw', align: 'center', title: '单位'},
                    {field: 'sku', align: 'center', title: '标识'}
                ]
            ],
            columns: [
                [
                    {field: 'zl', title: '重量', align: 'right',
                        formatter: function (value) {
                            var val = formatSlForHtml(value);
                            return val;
                        }},
                    {field: 'js', align: 'right', title: '件数'},
                    {field: 'jz', align: 'right', title: '件重'},
                    {field: 'kkdsl', align: 'right', title: '可开单重量',
                        formatter: function (value) {
                            var val = formatSlForHtml(value);
                            return val;
                        }},
                    {field: 'wsdj', align: 'right', title: '无税单价(元)',
                        formatter: function (value) {
                            var val = formatJeForHtml(value);
                            return val;
                        }
                    },
                    {field: 'wsje', align: 'right', title: '无税金额(元)',
                        formatter: function (value) {
                            var val = formatJeForHtml2(value);
                            return val;
                        }
                    },
                    {field: 'cgdh', align: 'center', title: '采购单号'},
                    {field: 'ck', align: 'center', title: '仓库'},
                    {field: "gys", align: 'center', title: "供应商", width: 150},
                    {field: 'sbgs', align: 'center', title: '申报公司'},
                    {field: "cggs", align: 'center', title: "采购公司"},
                    {field: "rkrq", align: 'center', title: "入库日期"}
                ]
            ],
            onSelect: function (rowIndex, rowData) {
                var data = $('#search-jgd-selected-jgd-grid').datagrid("getData");
                var flag = true;
                var flag1 = true;
                var flag2 = true;
                var flag3 = true;
                // 判断可开单数量是否小于0
                if (rowData.kkdsl <= 0) {
                    flag = false;
                } else {
                    // 判断是否重复添加
                    $.each(data.rows, function (i, val) {
                        if (val.khbm == rowData.khbm) {
                            flag1 = false;
                            return false;
                        }
                    })
                    // 判断申报公司是否相同
                    if(flag1){
                        $.each(data.rows, function (i, val) {
                            if (val.sbgs != rowData.sbgs) {
                                flag2 = false;
                                return false;
                            }
                        })
                    }
                    // 判断是否同一批次
                    if(flag1&&flag2){
                        $.each(data.rows, function (i, val) {
                            if (val.rkdh != rowData.rkdh) {
                                flag3 = false;
                                return false;
                            }
                        })
                    }
                }
                if (!flag) {
                    $.messager.alert('提示', "可开单重量不大于0!请重新选择", 'info');
                } else {
                    if (!flag1) {
                        $.messager.alert('提示', "选择的物资与已选过!请重新选择", 'info');
                    } else if(!flag2){
                        $.messager.alert('提示', "选择的物资必须申报公司相同!请重新选择", 'info');
                    }else if(!flag3){
                        $.messager.alert('提示', "选择的物资不是同一批次!请重新选择", 'info');
                    }else{
                        $(selectedGrid).datagrid('appendRow', rowData);
                    }
                }
            }
        });
    });

})

</script>
<div class="easyui-layout" data-options="fit:true">
    <div data-options="region:'north',split:true" style="height:375px">
        <div id="toolbar_jgd_search" style="height:auto">
            <div class="textcon">
                <table class="tb" style="margin-left: 12px;">
                    <tr>
                        <th>卡号：</th>
                        <td>
                            <input id="jgd_khbm" name="khbm" type="text" class="input-text">
                        </td>
                        <th>入库单号：</th>
                        <td>
                            <input id="jgd_rkdh" name="rhdh" type="text" class="input-text">
                        </td>
                        <th>物资编号:</th>
                        <td>
                            <input id="jgd_code" name="wzbm" type="text" class="input-text">
                        </td>
                    </tr>
                    <tr>
                        <th>规格型号：</th>
                        <td><input id="jgd_ggxh" name="ggxhmc" type="text" class="input-text"></td>
                        <th>物资名称：</th>
                        <td><input id="jgd_wzmc" name="wzmc" type="text" class="input-text"></td>
                        <th>仓&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;库：</th>
                        <td>
                            <input id="jgd_ckmc" name="ckmc" type="text" class="input-text">
                        </td>
                    </tr>
                    <tr>
                        <th>申报公司：</th>
                        <td>
                            <input id="jgd_sbgs" type="text" class=" input-text" class="input-text"/>
                        </td>
                        <th>采购公司：</th>
                        <td><input id="jgd_cggs" type="text" class=" input-text"
                                   data-options="valueField:'id',textField:'jc',panelWidth:'150',editable:'true'"/>
                        </td>
                        <td style=" padding-left:5px;"><input id="btn_jgd_search_init" type="button" value=""
                                                              class="submit"/>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
        <table id="jgd_into_grid"></table>
    </div>
    <div data-options="region:'center'">
        <table id="search-jgd-selected-jgd-grid">
        </table>
        <div id="search-jgd-menu" class="easyui-menu" style="width:120px;">
            <div name="delete">删除</div>
        </div>
    </div>
</div>